package de.ubschmidt.capi;

import java.nio.ByteBuffer;

/**
 * This message indicates the clearing of the physical channel identified
 * via the parameter PLCI. The parameter Reason indicates the cause
 * for this clearing.
 */
public class DisconnectInd
        extends CapiPassiveMessage
        implements PlciAccessor {

    private static final int OFFSET_PLCI = OFFSET_MESSAGE;
    private static final int OFFSET_REASON = OFFSET_MESSAGE + SIZE_OF_PLCI;

    /**
     * Create a new {@code DisconnectInd} message from the specified
     * {@code ByteBuffer}.
     *
     * @param byteBuffer The {@ByteBuffer} containing a DISCONNECT_IND message.
     */
    public DisconnectInd(final ByteBuffer byteBuffer) {
        super(byteBuffer);
    }

    /** {@inheritDoc} */
    public int getPlci() {
        return getByteBuffer().getInt(OFFSET_PLCI);
    }

    /**
     * Retrieve the {@code Reason} parameter.
     *
     * @return The {@code Reason} parameter.
     */
    public ErrorInfo getReason() {
        return ErrorInfo.valueOf(getByteBuffer().getShort(OFFSET_REASON));
    }

    /** {@inheritDoc} */
    @Override
    public String toString() {
        String s = String.format(
                "%s[%s plci=0x%08x reason=%s]",
                getClass().getSimpleName(),
                super.toString(),
                getPlci(),
                getReason());

        return s;
    }

}
